home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / files.swg / 0098_File date & time stamp unit.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  9.2 KB  |  392 lines

  1. { I made this to time stamp my programs with a time stamp giving the
  2.   version no.  Much of it is courtesy of SWAG and TP6's on line help.
  3.  
  4.   Use, improve, modify or whatever, but at one's own risk.
  5.  
  6.                              Albert L. Fowler.
  7.                              Kircaldy, Scotland                    }
  8. {------------------------------------------------------------------}
  9.  
  10. Program time_and_date_stamp_the_passed_file;      { stamp_it.pas }
  11.  
  12. (*  A file date and time stamping facility.
  13.  
  14.                  By A. L. Fowler 10th November 1996 *)
  15.  
  16. Uses
  17. Dos, Crt, List_A;      { for List_A.pas see below }
  18.  
  19. Var
  20.   hrs, mins    : Word;        { to set time stamp }
  21.  
  22.  
  23. Function LeadingZero (w : Word) : String;
  24. Var
  25.   s     : String;
  26.  
  27. Begin
  28.   Str (w : 0, s);
  29.   If Length (s) = 1 Then
  30.      s := '0' + s;
  31.   LeadingZero := s;
  32. End;
  33.  
  34.  
  35. Procedure header;
  36. Begin
  37.   GotoXY (4, 1);
  38.   TextColor (14);
  39.   Write ('STAMP_IT A File Time & Date Updating Facility.  By  A. L. Fowler. 1996');
  40.   NormVideo;
  41. End;
  42.  
  43.  
  44. Procedure check_passed_param;
  45.  
  46. Var
  47.   s     : String [80];
  48.   f     : Text;
  49.   ft    : LongInt;            { For Get/SetFTime }
  50.   dt    : DateTime;        { For Pack/UnpackTime }
  51.  
  52.  
  53. Begin
  54.   GetDir (0, s);             { 0 = Current drive }
  55.   If ( ParamCount <> 1 ) Then
  56.      Begin
  57.      header;
  58.      GotoXY (14, 4);
  59.      TextColor (10);
  60.      Write ('STAMP_IT  Does not have a following filename');
  61.      GotoXY (8, 6);
  62.      Write ('e.g.  ', s, '\STAMP_IT  [drive:][path][filename]');
  63.      NormVideo;
  64.      GotoXY (1, 24);
  65.      Halt (1);
  66.      End;
  67.  
  68.   If Not FileExists (ParamStr (1) ) Then
  69.      Begin
  70.      header;
  71.      TextColor (10);
  72.      GotoXY (8, 4);
  73.      Write ('  File  ', UpperCase (ParamStr (1) ), '  not found.');
  74.      GotoXY (8, 6);
  75.      Write ('Syntax  ', s, '\STAMP_IT  [drive:][path][filename]');
  76.      NormVideo;
  77.      GotoXY (1, 24);
  78.      Halt (2);
  79.      End
  80.   Else
  81.      GotoXY (4, 3);
  82.   TextColor (11);
  83.   Write (UpperCase (ParamStr (1) ) );
  84.  
  85.   Assign (f, ParamStr (1) );
  86.   Reset (f);                 { Open specified File }
  87.   GetFTime (f, ft);            { Get creation time }
  88.   UnpackTime (ft, dt);
  89.   With dt Do
  90.        Begin
  91.        Write (' [ Time Stamped ', LeadingZero (hour), ':', LeadingZero (min),
  92.        ':', LeadingZero (sec), ' & Dated ', day, '-', month, '-', year, ' ]');
  93.        End;
  94.   GotoXY (4, 4);
  95.   WriteLn ('Will be Stamped with Today''s Date & given a Time Stamp you can choose.');
  96.   NormVideo;
  97. End;
  98.  
  99.  
  100. Procedure choose_time_stamp;
  101.  
  102. Var
  103.   yn, ap, Confirm      : Char;
  104.   _h,  _m    : String [3];
  105.   c, h, inp  : Integer;
  106.  
  107. Label
  108.   again, h1, m1;
  109.  
  110. Begin
  111.   hrs  := 0;
  112.   mins := 0;
  113.   _h  := '0';
  114.   _m  := '0';
  115.  
  116.   FlushKeyBuffer;
  117.   again :
  118.   GotoXY (4, 6);
  119.   TextColor (10);
  120.   Write ('Is the Default Time Stamp of 12:01a Acceptable?  Y/N  ');
  121.   CursorOff;
  122.   yn := ReadKey;
  123.   If yn = #13 Then
  124.      Goto again;
  125.   If yn = #27 Then
  126.      Begin
  127.      CursorOn;
  128.      NormVideo;
  129.      GotoXY(1, 24);
  130.      Halt (3);
  131.      End;
  132.   If Not ( UpCase (yn) In [ 'N', 'Y'] )  Then
  133.      Begin                       { Only accept y or n }
  134.      Alarm;
  135.      Goto again;
  136.      End
  137.   Else
  138.  
  139.      If UpCase (yn) = 'N' Then            { User time stamp requested }
  140.  
  141.     Begin   { of user input }
  142.  
  143.     h1 :        { Do not accept nul or error input }
  144.     GotoXY (4, 8);
  145.     Write ('Enter hours. ');
  146.     ReadLn (_h);
  147.  
  148.     If _h  = '0' Then
  149.        _h := '00';                { ok its a bodge }
  150.  
  151.     If Ord (_h [0]) >= 3 Then     { oversize hours inputs }
  152.        Begin
  153.        GotoXY (17, 8);
  154.        Write ('           Input not recognised, enter again.   ');
  155.        Alarm;
  156.        Goto h1;
  157.        End;
  158.  
  159.     { First delete any leading zeros in the hours string }
  160.     If (Ord (_h [0]) >=  1) And (_h [1] = '0') Then
  161.        _h := Copy (_h, 2, Ord (_h [0]) - 1);
  162.  
  163.     Val (_h, hrs,  c);                    { Convert to hrs }
  164.     If ( c <> 0 ) Or ( hrs > 23 ) Then    { Rubbish inputs }
  165.        Begin
  166.        GotoXY (17, 8);
  167.        Write ('           Hours  in the Range 0 to 23 please.  ');
  168.        Alarm;
  169.        Goto h1;
  170.        End;
  171.  
  172.     m1 :            { Do not accept nul or error input }
  173.     GotoXY (4, 10);
  174.     Write ('Enter mins.  ');
  175.     ReadLn (_m);
  176.  
  177.     If _m  = '0' Then
  178.        _m := '00';              { ok its another bodge }
  179.  
  180.     If Ord (_m [0]) >= 3 Then   { oversize mins inputs }
  181.        Begin
  182.        GotoXY (17, 10);
  183.        Write ('           Input not recognised, enter again.   ');
  184.        Alarm;
  185.        Goto m1;
  186.        End;
  187.  
  188.     { First delete any leading zeros in the mins string }
  189.     If (Ord (_m [0]) >=  1) And (_m [1] = '0') Then
  190.        _m := Copy (_m, 2, Ord (_m [0]) - 1);
  191.  
  192.     Val (_m, mins, c);                            { Convert to min }
  193.     If ( c <> 0 ) Or ( mins > 59 ) Then           { Rubbish inputs }
  194.        Begin
  195.        GotoXY (17, 10);
  196.        Write ('           Minutes in the Range 0 to 59 please.  ');
  197.        Alarm;
  198.        Goto m1;
  199.        End;
  200.     End;    { of user input }
  201.  
  202.   If UpCase (yn) = 'Y' Then       { User accepts 12:01a }
  203.      Begin
  204.      _h  := '0';
  205.      Val (_h, hrs,  c);        { Convert to hrs }
  206.      _m  := '1';
  207.      Val (_m, mins, c);        { Convert to min }
  208.      End;
  209.  
  210.   Begin                 { Section produces time in  am pm format }
  211.   If hrs < 12 Then      { now convert hrs & mins for user to see }
  212.      Begin
  213.      ap := 'a';
  214.      If hrs = 0  Then
  215.     _h := '12';
  216.      End
  217.   Else
  218.      Begin
  219.      ap := 'p';
  220.      h := hrs - 12;                { to display in am pm format }
  221.      Str (h, _h);
  222.      End;
  223.   If mins < 10 Then
  224.      _m := '0' + _m;
  225.   End;    { am pm on screen information }
  226.  
  227.   GotoXY (4, 12);
  228.   Write ('File will be Time Stamped ', _h, ':', _m, ap);
  229.  
  230.   GotoXY (4, 14);
  231.   Write ('Is this acceptable? . . . . Y/N ');
  232.   Confirm := ReadKey;
  233.  
  234.   If (Confirm = #27) Or (Confirm = #110) Or (Confirm = #78) Then
  235.      Begin                     { Esc , N or n pressed }
  236.      GotoXY (4, 14);
  237.      Write ('Exit confirmed, file has not been changed.');
  238.      CursorOn;
  239.      GotoXY (1, 24);
  240.      NormVideo;
  241.      Halt (4);
  242.      End;
  243.  
  244.   If (Confirm = #89) Or (Confirm = #121) Then
  245.      GotoXY (1, 24);
  246.   CursorOn;
  247.   NormVideo;
  248. End;
  249.  
  250.  
  251. Procedure stamp_file;
  252.  
  253. Var
  254.   f     : Text;
  255.   ftime : LongInt;                    { For Get/SetFTime }
  256.   dt    : DateTime;                   { For Pack/UnpackTime }
  257.   year, month, day, DofW    : Word;   { for GetDate }
  258.  
  259. Begin
  260.   Assign (f, ParamStr (1) );
  261.   GetDate (year, month, day, DofW);     { Today''s Date  }
  262.   Reset (f);                            { Open existing File }
  263.   GetFTime (f, ftime);                  { Get old creation time }
  264.   UnpackTime (ftime, dt);
  265.   GotoXY (4, 17);
  266.   TextColor (11);
  267.   With dt Do
  268.        Begin
  269.        Write ('Old File TimeStamp was:  ', LeadingZero (hour), ':', LeadingZero
  270.        (min), ':', LeadingZero (sec), '    Dated:  ', day, '-', month, '-', year);
  271.  
  272.        GetDate (year, month, day, DofW);   { Again to Set/Confirm today's date }
  273.        hour := hrs;
  274.        min  := mins;             { These for chosen time stamp }
  275.        sec  := 0;
  276.  
  277.        PackTime (dt, ftime);
  278.        Reset (f);
  279.        { Re-open File For reading otherwise, close will update time }
  280.        SetFTime (f, ftime);
  281.  
  282.        GetFTime (f, ftime);                { Get new creation time }
  283.        UnpackTime (ftime, dt);
  284.        GotoXY (4, 19);
  285.        With dt Do
  286.         Begin
  287.         Write ('New File TimeStamp  is:  ', LeadingZero (hour), ':',
  288.         LeadingZero (min),
  289.         ':', LeadingZero (sec), '    Dated:  ', day, '-', month, '-', year );
  290.         End;
  291.        End;
  292.   GotoXY (1, 24);
  293.   Close (f);        { Close File }
  294.   NormVideo;
  295. End;
  296.  
  297.  
  298. Begin
  299.   ClrScr;
  300.   check_passed_param;
  301.   header;
  302.   choose_time_stamp;
  303.   stamp_file;
  304. End.
  305.  
  306. {-------------------------------------------------------------------------}
  307. Unit LIST_A;
  308.  
  309. (* LIST_A a simple list, used in STAMPIT, etc. *)
  310.  
  311. Interface
  312. Uses Crt, Dos;
  313.  
  314. Procedure CursorOff;
  315. Procedure CursorOn;
  316. Procedure FlushKeyBuffer;
  317. Procedure Alarm;
  318. Function FileExists (FileName : String) : Boolean;
  319. Function UpperCase (s : String) : String;
  320.  
  321.  
  322.   Implementation
  323.  
  324. {*****************************************************************************}
  325.  
  326. Procedure CursorOff;
  327.   Assembler;
  328.   Asm
  329.   MOV   ax, $0100
  330.   MOV   cx, $2607
  331.   Int   $10
  332. End;
  333.  
  334. Procedure CursorOn;
  335.   Assembler;
  336.   Asm
  337.   MOV   ax, $0100
  338.   MOV   cx, $0506
  339.   Int   $10
  340. End;
  341.  
  342. Procedure FlushKeyBuffer;
  343. Var
  344.   recpack : Registers;
  345. Begin
  346.   With recpack Do
  347.        Begin
  348.        ax := ($0c ShL 8) Or 6;
  349.        dx := $00ff;
  350.        End;
  351.   Intr ($21, recpack);
  352. End;     {FlushKeyBuffer}
  353.  
  354.  
  355. Function FileExists (FileName : String) : Boolean;
  356. { Returns True if file exists; otherwise, it returns  False.
  357.        Closes the file and exists.  }
  358. Var
  359.   f : File;
  360.  
  361. Begin
  362.   {$I-}
  363.   Assign (f, FileName);
  364.   Reset (f);
  365.   Close (f);
  366.   {$I+}
  367.   FileExists := (IOResult = 0) And (FileName <> '');
  368. End;      { FileExists }
  369.  
  370.  
  371. Function UpperCase (s : String) : String;
  372. Var
  373.   I : Integer;
  374. Begin
  375.   For I := 1 To Ord (s [0]) Do
  376.       If s [I] In ['a'..'z'] Then
  377.      Dec (s [I], 32);
  378.   UpperCase := s;
  379. End;
  380.  
  381. Procedure Alarm;
  382. Begin
  383.   Sound (466);
  384.   Delay (150);
  385.   Sound (349);
  386.   Delay (200);
  387.   NoSound;
  388. End;
  389.  
  390. End.
  391.  
  392.